<template>
	<view class="content">
		<uni-forms ref="baseForm">
			<view class="item">
				<uni-data-checkbox :localdata="sexs" v-model="tab" @change="SurePayWay" /></uni-forms-item>
			</view>
		</uni-forms>
		<view class="item" v-if="tab == 2">
			<view class="title">请上传张电汇发票</view>
			<uni-file-picker v-model="imageValue" @select="UpdateImg" :disabled="img_disabled" fileMediatype="image" :limit="1"
				mode="grid" ref="files" :auto-upload="false" />
			<button size="mini" :disabled="Upload_disabled" class="btn" @click="ElectronicRemittance">上传文件</button>
		</view>
	</view>
</template>

<script>
import {
	TransactionPaystagesID,
	GroupWeChatNativeAction,
	GroupElectronicRemittance,
	ElectronicPIC
} from '@/api/group.js'
const app = getApp();
export default {
	data() {
		return {
			title: '加载中...请稍后',
			loading: false,
			img: [],
			Data: [],
			formData: {},
			radioValue: 1,
			img_disabled: true,
			Upload_disabled: true,
			Group_Paystages_SerialNumber: '',
			Group_Transaction_SerialNumber: '',
			Enterprise_Info_SerialNumber: "",
			Amount: '',
			Picture_URL: '',
			Collection_SerialNumber: "",
			dislogChoosePay: false,
			dislogPayCode: false,
			dislogPayTicket: false,
			codeurl: '',
			Domain_IMG: app.globalData.Domain_IMG,
			ThemeColors: app.globalData.ThemeColors,
			PaystagesData: {},
			current: -1,
			tab: 0,
			sexs: [{
				text: '微信',
				value: 0
			}, {
				text: '支付宝',
				value: 1
			}, {
				text: '电汇',
				value: 2
			}],
			imageValue: [],
			Time: null,
		};
	},
	onLoad(options) {
		this.Group_Paystages_SerialNumber = options.Group_Paystages_SerialNumber;
		this.Amount = options.Amount;
	},
	methods: {
		//支付方式
		SurePayWay() {
			if (this.tab == 0) {
				// 微信支付
				if (this.codeurl == '') {
					let data = {
						Data: this.Group_Paystages_SerialNumber,
						Order_Type: '3',
						Collection_Type: '微信支付'
					}
					this.CodeUrl(data)
				} else {
					this.dislogPayCode = true
				}
			} else if (this.tab == 1) {
				//支付宝支付
			} else {
				//电汇支付
				let data = {
					Data: this.Group_Paystages_SerialNumber,
					Order_Type: '3',
					Collection_Type: '电子汇账'
				}
				this.dislogPayTicket = true
				this.CodeUrl(data)
			}
			this.dislogChoosePay = false
		},
		//上传发票
		async CodeUrl(data) {
			this.dislogPayCode = true
			const res = await GroupWeChatNativeAction({
				data
			})
			if (res.redirect && res.redirect == 'REDIRECT' || res.REDIRECT == 'REDIRECT') {
				console.log(res.redirect);
			} else {
				console.log(res)
				if (res.code_url) {
					this.codeurl = res.code_url
					this.Time = setInterval(() => {
						let Data = {
							Data: 'collection',
							Primary_Key_Name: 'Collection_SerialNumber',
							Primary_Key: res.Data,
							isNullEnterprise: true
						}
						this.getPayNotice(Data)
					}, 1000)
					return
				}
				if (res.Data) {
					this.Collection_SerialNumber = res.Data
					this.title = '请上传一张电汇发票'
					this.img_disabled = false;
				}
			}
		},
		//微信
		async getPayNotice(data) {
			const res = await getInquiryIDOriginal({
				data
			})
			console.log(res)
			if (res[0].Row_Status == '1') {
				clearInterval(this.Time)
				this.Time = null
				console.log('已付款');
				// Message({
				// 	type: 'success',
				// 	message: '已付款'
				// })
				// this.CompletePayfor()
			}
		},

		//图片转换
		UpdateImg(e) {
			console.log(e)
			let Picture_URL
			//图片转base64
			uni.request({
				url: e.tempFilePaths[0],
				method: 'GET',
				responseType: 'arraybuffer',
				success: ress => {
					Picture_URL = uni.arrayBufferToBase64(ress.data); //把arraybuffer转成base64
					let data = {
						Data: 'data:image/jpeg;base64,' + Picture_URL,
						DataLength: e.tempFiles[0].size,
						FileName: e.tempFiles[0].name
					}
					this.CheckImg(data)
					console.log(data);
				},
				fail: (e) => {
					console.log("图片转换失败");
				}
			})
		},

		async CheckImg(data) {
			const res = await ElectronicPIC({
				data,
			})
			if (res.redirect && res.redirect == 'REDIRECT' || res.REDIRECT == 'REDIRECT') {
				console.log(res.redirect);
			} else if (res.Status == 'success') {
				console.log(res);
				this.Picture_URL = res.Picture_Url
				this.Upload_disabled = false;
			} else {
				console.log('不是一张有效的电汇凭证');
				// Message({
				// 	message: '不是一张有效的电汇凭证',
				// 	type: 'error'
				// })
			}
		},

		//提交
		async ElectronicRemittance() {
			this.loading = true
			let data = {
				Picture_URL: this.Picture_URL,
				Collection_SerialNumber: this.Collection_SerialNumber,
				Purchase_Amount: this.Amount,
			}
			const res = await GroupElectronicRemittance({
				data,
			})
			if (res.redirect && res.redirect == 'REDIRECT') {
				console.log(res.redirect);
			} else {
				console.log(res)
				this.loading = false
				console.log('提交成功！');
				uni.navigateBack();
				// Message({
				// 	message: '提交成功！',
				// 	type: 'success'
				// })
			}
		},
		upload() {
		}
	}
}
</script>

<style lang="scss">
.content {
	padding: 20rpx;
	box-sizing: border-box;
	background: #f5f5f5;
	height: calc(100vh);

	.item {
		border-radius: 20rpx;
		background-color: #fff;
		padding: 20rpx;
		box-sizing: border-box;
		margin: 0 0 30rpx 0;

		/deep/ .checklist-group {
			justify-content: space-around;
		}

		.title {
			text-align: center;
			margin: 10rpx 0;
			font-size: 24rpx;
		}

		.btn {
			margin: 20rpx 0 0 0;
		}
	}


}
</style>
